define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        config = require("./setting"),
        method = require("./method");
    $("[data-toggle=tooltip]").tooltip();

    exports.init = function(el) {


        var $chosen = $('[data-toggle="chosen"]', el);
        $chosen.length > 0 && require.async(['chosen', 'style/plugins/chosen/chosen.min.css'], function() {
            $chosen.each(function() {
                var t = $(this),
                    r = t.data("nosearch"),
                    n = {},
                    i = t.data("maxlength"),
                    p = t.data('remote') || t.attr('href');
                i && (n.max_selected_options = i);
                r && (n.disable_search_threshold = 9999999);
                n.hide = false;
                n.no_results_text = "找不到";
                if (p) {
                    console.log(p)
                    $.ajax(p, {
                        type: "post",
                        dataType: "json"
                    }).done(function(d) {
                        $.each(d, function(index, item) {
                            var tmp = '<option value="{1}">{0}</option>';
                            t.append(tmp.format(item.text, item.value))
                        });
                        t.chosen(n)
                    }).fail(function() {
                        config.msg.info("网络异常")
                    });
                } else {
                    t.chosen(n)
                }



            });

        })
        var $select2 = $('[data-toggle="select2"]', el);
        $select2.length > 0 && require.async(['select2', 'style/plugins/select2/select2.min.css'], function() {
            $select2.each(function() {
                var t = $(this),
                    n = {},
                    c = t.data('maxcount'),
                    o = t.data('chang'),
                    r = t.data('remote') || t.attr('href');
                c && (n.maximumSelectionSize = c);
                if (o) {
                    switch (o) {
                        case "submit":
                        {
                            t.on("change", function(e) {
                                t.closest("form ").submit();
                            })
                        }
                    }
                };
                t.select2(n)
            })
        });
        var $reportrange = $('[data-toggle="reportrange"]', el);
        $reportrange.length > 0 && require.async(['moment', 'daterangepicker', 'style/plugins/daterangepicker/daterangepicker-bs3.min.css'], function() {
            $reportrange.each(function() {
                var $this = $(this),
                    $selected = $this.data("selected");
                var $span_date = $this.find("span");
                var time = $span_date.text().split("-");
                var a = moment(time[0]);
                var b = moment(time[1]);
                var d = b.diff(a, "days");
                $this.daterangepicker({
                        format: "YYYY/MM/DD HH:mm",
                        ranges: {
                            '今天': [moment(), moment()],
                            '昨天': [moment().subtract('days', 1), moment().subtract('days', 1)],
                            '最近7天': [moment().subtract('days', 6), moment()],
                            '最近30天': [moment().subtract('days', 29), moment()],
                            '这个月': [moment().startOf('month'), moment().endOf('month')],
                            '上个月': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
                        },
                        startDate: a || moment().subtract('days', d),
                        endDate: b || moment()
                    },
                    function(start, end) {
                        var date = start.format('YYYY/MM/DD') + ' - ' + end.format('YYYY/MM/DD');
                        $('span', $this).html(date);
                        $('input[type="hidden"]', $this).val(date);
                        var even = $this.data("change")
                        if (even) {
                            switch (even) {
                                case "submit":
                                {
                                    $this.closest("form ").submit();
                                }
                            }
                        };
                    }
                );
            });

        })

        var $combodate = $('[data-toggle="combodate"]', el);
        $combodate.length > 0 && require.async(['combodate'], function() {
            $combodate.each(function() {
                var $this = $(this);
                $this.combodate({
                    minuteStep: 1
                });
            });

        })
        var $clockpicker = $('[data-toggle="clockpicker"]', el);
        $clockpicker.length > 0 && require.async(['clockpicker', 'style/plugins/clockpicker/clockpicker.min.css'], function() {
            $clockpicker.each(function() {
                var $this = $(this),
                    $input = $this.find("input[type='text']:first"),
                    placement = $this.data("placement") || "top",
                    align = $this.data("align") || "left",
                    autoclose = $this.data("autoclose") || false,
                    than = $input.data("than"),
                    less = $input.data("less"),
                    day = $input.data("day"),
                    minutes5 = $this.data("minutes5") || false;
                $this.clockpicker({
                    placement: placement,
                    align: align,
                    minutes5: minutes5,
                    autoclose: autoclose,
                    afterDone: function() {
                        var $day = $this.closest('.form-group').find(day),
                            type = $day.find("option:selected").data("type");
                        if (type) {
                            return false;
                        }

                        if (than) {
                            var $pre = $(than),
                                pval = $pre.val(),
                                val = $input.val();
                            if (val) {
                                var arr2 = val.split(":");
                                if (pval) {
                                    var arr1 = pval.split(":"),
                                        $p = $input.closest('.form-group'),
                                        msg = $input.data("thanMsg") || "时间";
                                    var hour2 = arr2[0] - 0,
                                        time2 = arr2[1] - 0,
                                        hour1 = arr1[0] - 0,
                                        time1 = arr1[1] - 0;
                                    if (hour2 - hour1 < 0) {
                                        $input.removeClass('error');
                                        $p.find(".js_validate_error").remove();
                                        $p.append("<span class='error help-block js_validate_error'>" + msg + "设置有误，必须大于" + pval + "！</span>");
                                        $input.addClass('error');
                                    } else if (hour2 - hour1 == 0) {
                                        if (time2 - time1 <= 0) {
                                            $input.removeClass('error');
                                            $p.find(".js_validate_error").remove();
                                            $p.append("<span class='error help-block js_validate_error'>" + msg + "设置有误，必须大于" + pval + "！</span>");
                                            $input.addClass('error');
                                        } else {
                                            $input.removeClass('error');
                                            $pre.removeClass('error');
                                            $p.find(".js_validate_error").remove();
                                        }
                                    } else {
                                        $input.removeClass('error');
                                        $pre.removeClass('error');
                                        $p.find(".js_validate_error").remove();
                                    }
                                }
                            }
                        }
                        if (less) {
                            var $next = $(less),
                                nval = $next.val(),
                                val = $input.val();
                            if (val) {
                                var arr2 = val.split(":");
                                if (nval) {
                                    var arr1 = nval.split(":"),
                                        $p = $input.closest('.form-group'),
                                        msg = $input.data("lessMsg") || "时间";
                                    var hour2 = arr2[0] - 0,
                                        time2 = arr2[1] - 0,
                                        hour1 = arr1[0] - 0,
                                        time1 = arr1[1] - 0;
                                    if (hour2 - hour1 > 0) {
                                        $input.removeClass('error');
                                        $p.find(".js_validate_error").remove();
                                        $p.append("<span class='error help-block js_validate_error'>" + msg + "设置有误，必须小于" + nval + "！</span>");
                                        $input.addClass('error');
                                    } else if (hour2 - hour1 == 0) {
                                        if (time2 - time1 >= 0) {
                                            $input.removeClass('error');
                                            $p.find(".js_validate_error").remove();
                                            $p.append("<span class='error help-block js_validate_error'>" + msg + "设置有误，必须小于" + nval + "！</span>");
                                            $input.addClass('error');
                                        } else {
                                            $input.removeClass('error');
                                            $next.removeClass('error');
                                            $p.find(".js_validate_error").remove();
                                        }
                                    } else {
                                        $input.removeClass('error');
                                        $next.removeClass('error');
                                        $p.find(".js_validate_error").remove();
                                    }
                                }
                            }
                        }


                    }
                });
                var next = $this.next("span.input-group-addon");
                var prev = $this.prev("span.input-group-addon");
                next.add(prev).on("click", function() {
                    $this.clockpicker('show');
                });
            });

        })
        var $daterangepicker = $('[data-toggle="daterangepicker"]', el);
        $daterangepicker.length > 0 && require.async(['moment', 'daterangepicker', 'style/plugins/daterangepicker/daterangepicker-bs3.min.css'], function() {
            $daterangepicker.each(function() {
                var $this = $(this),
                    opens = $this.data("opens") || "right",
                    mindate = $this.data("minDate") || false,
                    maxdate = $this.data("maxDate") || false,
                    format = $this.data("format") || "YYYY/MM/DD HH:mm";

                $this.daterangepicker({
                        timePicker: !0,
                        opens: opens,
                        timePicker12Hour: false,
                        minDate: mindate,
                        maxDate: maxdate,
                        format: format
                    },
                    function(start, end) {
                        var even = $this.data("change")
                        if (even) {
                            switch (even) {
                                case "submit":
                                {
                                    $this.closest("form ").submit();
                                }
                            }
                        };
                    });
                var next = $this.next("span.input-group-addon");
                var prev = $this.prev("span.input-group-addon");
                next.add(prev).on("click", function() {
                    $this.trigger("click");
                });
            });

        })

        var $dateRange = $('[data-toggle="dateRange"]', el);
        $dateRange.length > 0 && require.async(['moment', 'daterangepicker', 'style/plugins/daterangepicker/daterangepicker-bs3.min.css'], function() {
            $dateRange.each(function() {
                var $this = $(this),
                    dataFormat = $(this).data("format"),
                    timePicker = $(this).data("timePicker");
                $this.daterangepicker({
                    timePicker: timePicker,
                    format: dataFormat
                });
            });
        })


        var $switch = $('[data-toggle="switch"]', el);
        $switch.length > 0 && require.async(['switch', 'style/plugins/switch/switch.min.css'], function() {
            $.each($switch, function(index, val) {
                var $this = $(this),
                    p = $this.data("path");
                $this.bootstrapSwitch();
                p && (function() {
                    $this.on("switchChange.bootstrapSwitch", function(event, state) {
                        method.postd(p, function(d) {
                            if (config.issucceed(d)) {
                                //config.msg.suc(d.message || config.lang.modifySuccess, d.url)
                            } else {
                                config.msg.error(d.message || config.lang.modifyError, d.url)
                            }
                        }, {
                            state: state
                        })
                    })

                })()

            });
        })
        var $datepicker = $('[data-toggle="datepicker"]', el);
        $datepicker.length > 0 && require.async(['datepicker', 'style/plugins/datepicker/datepicker3.min.css'], function() {
            $datepicker.each(function() {
                var $this = $(this),
                    start = $this.data("startdate"),
                    startv = "",
                    format = $this.data("format") || "yyyy-mm-dd",
                    minView = 0,
                    end = $this.data("endDate"),
                    endv = "";
                if (start == "now" || start == "") {
                    startv = new Date();
                } else {
                    startv = new Date(start);
                }
                if (end == "now" || end == "") {
                    endv = new Date();
                } else {
                    endv = new Date(end);
                }
                if (format == "yyyy-MM") {
                    minView = 1;
                } else if (format == "yyyy") {
                    minView = 2;
                }
                $this.datepicker({
                    startDate: startv,
                    endDate: endv,
                    minViewMode: minView,
                    format: format
                }).on('changeDate', function(ev) {
                    $this.blur();
                });
                var next = $this.next("span.input-group-addon");
                var prev = $this.prev("span.input-group-addon");
                next.add(prev).on("click", function() {
                    $this.datepicker('show');
                });
            });

        });
        var $datepickersub = $('[data-toggle="datepickersub"]');
        $datepickersub.length > 0 && require.async(['datepicker', 'style/plugins/datepicker/datepicker3.min.css'], function() {
            $datepickersub.each(function() {
                var $this = $(this),
                    start = $this.data("startdate"),
                    startv = "",
                    format = $this.data("format") || "yyyy-mm-dd",
                    minView = 0,
                    end = $this.data("endDate"),
                    endv = "";
                if (start == "now" || start == "") {
                    startv = new Date();
                } else {
                    startv = new Date(start);
                }
                if (end == "now" || end == "") {
                    endv = new Date();
                } else {
                    endv = new Date(end);
                }
                if (format == "yyyy-mm") {
                    minView = 1;
                } else if (format == "yyyy") {
                    minView = 2;
                }
                $this.datepicker({
                    startDate: startv,
                    endDate: endv,
                    minViewMode: minView,
                    format: format
                }).on('changeDate', function(ev) {
                    var date = ev.date.format(format);
                    $('span', $this).html(date);
                    $('input[type="hidden"]', $this).val(date);
                    var even = $this.data("change");
                    if (even) {
                        switch (even) {
                            case "submit":
                            {
                                $this.closest("form ").submit();
                            }
                        }
                    };
                });;

            });

        });
        var $datetimepicker = $('[data-toggle="datetimepicker"]', el);
        $datetimepicker.length > 0 && require.async(['moment', 'datetimepicker', 'style/plugins/datetimepicker/datetimepicker.min.css'], function() {
            $datetimepicker.each(function() {
                var $this = $(this),
                    minutestep = $this.data("minutestep") || 1,
                    position = $this.data("position") || "bottom-left";
                $this.datetimepicker({
                    language: "zh-CN",
                    minuteStep: minutestep,
                    pickerPosition: position,
                    autoclose: true

                });
                var next = $this.next("span.input-group-addon");
                var prev = $this.prev("span.input-group-addon");
                next.add(prev).on("click", function() {
                    $this.datetimepicker('show');
                });
            });

        })
        var $fileupload = $('[data-provides="fileinput"]', el);
        $fileupload.length > 0 && require.async('fileupload');



        var $ajaxfile = $('[data-toggle="ajaxfile"]', el);
        $ajaxfile.length > 0 && require.async('ajaxfileupload', function() {
            $.each($ajaxfile, function(index, val) {
                var $this = $(this),
                    data = $this.data();
                $(this).ajaxfile({
                    path: data.path
                }, function(d) {
                    if (d.error == 0) {
                        if (data.target) {
                            $('' + data.target + '').attr("src", d.url)
                        };
                    } else {
                        config.msg.error(d.message || config.lang.removeError, d.url)
                    }
                    if (d.refresh) {
                        var mu = new method.URL(window.parent.location.href);
                        mu.set("url", window.location.href)
                        window.parent.location = mu.url()
                    };



                })

            });
        });

        var $fresco = $(".js_fresco", el);
        $fresco.length > 0 && require.async(['fresco', '/css/plugins/fresco/fresco.min.css'], function(a) {
            var imglist = $.map($fresco, function(item, index) {
                return {
                    options: {
                        thumbnail: $(item).attr("src")
                    },
                    url: $(item).data("src")
                };
            });
            // $fresco.on('click', function(e) {
            //     e && e.stopPropagation();
            //     e&&e.preventDefault()
            //     console.log(e.target)
            //     Fresco.show(imglist, {
            //         ui: 'inside'
            //     })
            // });
            $fresco.closest('li').on("click", function() {
                var index = $(this).index();
                Fresco.show(imglist, {
                    ui: 'inside'
                }, index + 1)
            })
        });

        var $frescos = $('[data-toggle="frescos"]', el);
        $frescos.length > 0 && require.async(['fresco', '/css/plugins/fresco/fresco.min.css'], function(a) {
            $frescos.each(function() {
                var $this = $(this),
                    $img = $(".js_frescos", $this);
                var imglist = $.map($img, function(item, index) {
                    return {
                        options: {
                            thumbnail: $(item).attr("src")
                        },
                        url: $(item).data("src")
                    };
                });
                $img.closest('li').on("click", function() {
                    var index = $(this).index();
                    Fresco.show(imglist, {
                        ui: 'inside'
                    }, index + 1)
                })
            });

        });
        var $alert_big_imgs = $('.alert_big_imgs', el);
        $alert_big_imgs.length > 0 && require.async(['dist/rotate/init.js', '/css/plugins/alert_img/skin.min.css'], function(rotate) {
            rotate.init(el);
        });

        var $location = $('[data-toggle="location"]', el);
        if ($location.length > 0) {
            require.async("dist/location/init.js", function(d) {

                $.each($location, function() {
                    var loc = $(this);
                    var $provinces = $('[data-location="provinces"]', loc)
                    var $city = $('[data-location="city"]', loc)
                    var $district = $('[data-location="district"]', loc)
                    var sel = new d.select({
                        data: d.data
                    });
                    sel.bind($provinces);
                    sel.bind($city);
                    sel.bind($district);
                });

            })
        };

        var $select_level2 = $('[data-toggle="select_level2"]', el);
        if ($select_level2.length > 0) {
            var $form = $("form.form-validate", el);
            require.async("dist/location/init.js", function(d) {
                var $select_level_1 = $('[data-location="select_level_1"]', $form)
                var $select_level_2 = $('[data-location="select_level_2"]', $form)
                var sel = new d.select({
                    data: window.select_level2_data
                });
                sel.bind($select_level_1);
                sel.bind($select_level_2);
            })
        };

        var $copy = $(".js_copy_text", el);
        $copy.length > 0 && require.async('zeroclipboard', function(a) {
            ZeroClipboard.setDefaults({
                moviePath: "/assets/swf/zeroclipboard.swf"
            });

            $.each($copy, function(i) {
                var $id = "copy_button{0}".format(i),
                    _tmp = '  <button class="btn btn-default js_copy" id="{0}" type="button" data-clipboard-target="{2}" data-clipboard-text="{1}"><i class="icon-copy"><\/i>复制<\/button>  <span class="alert copy-success  alert-success hd "  >复制成功,请粘帖到您需要的地方<\/span>',
                    $this = $(this),
                    v = $this.text().trim(),
                    $iid = $this.attr("id");
                // if (/[a-zA-z]+:\/\/[^\s]*/i.test(v)) {
                //     var _atmp = '<a href="{0}" target="_blank" title="点击新窗口打开">{0}<\/a>';
                //     $this.html(_atmp.format(v))
                // };
                if ($iid) {
                    $this.after(_tmp.format($id, "", $iid))
                } else {
                    $this.append(_tmp.format($id, v))
                };
                var el = $("#" + $id);
                var clip = new ZeroClipboard(el);
                var success = function(el) {
                    var span = el.nextAll("span.copy-success");
                    span.show();
                    setTimeout(function() {
                        span.fadeOut()
                    }, 1000)
                };
                clip.on('complete', function(client, args) {
                    success($(client.options.button))
                })
            })
            $(document).on("click", "button.js_copy", function() {
                var $this = $(this),
                    v = $this.data("clipboard-text").trim();
                if ($.browser.msie) {
                    window.clipboardData.setData("Text", v);
                    success($this)
                } else {
                    prompt('按下 Ctrl+C 复制到剪贴板', v)
                }
            })

        });


        window.wm.tokeninputtree = function(elements) {
            require.async(['jstree', 'tokeninput', 'style/plugins/tokeninput/tokeninput.min.css', 'style/plugins/jstree/jstree.min.css'], function() {
                $.each(elements, function(index, val) {
                    var $this = $(this),
                        path = $this.data("path"),
                        contact = $this.data("contact"),
                        selected = $this.data("selected"),
                        selectedContact = $this.data("selectedContact"),
                        tokenLimit = $this.data("tokenLimit"),
                        part = $this.data("part"),
                        submit = $this.data("submit"),
                        organization=$this.data("organization"),
                        member=$this.data("member"),
                        submitcss="";
                    if(submit){
                        submitcss=submit;
                    }else{
                        submitcss="js_no_submit_css";
                        submit="js_space_tree_placeholder"
                    }
                    var selectedContact_Checked = function(data) {
                        var selected_arr=[];
                        if(selectedContact){
                            selected_arr= selectedContact.split(',');
                        }
                        $.each(data, function(index, item) {
                            if ($.inArray(item.id, selected_arr) > -1) {
                                $this.tokenInput("add", item)
                            }
                        });
                    }
                    if (path) {
                        method.get(path, function(result) {
                            $this.tokenInputTree($.tokenInputConverter.forparty2token(result), $.tokenInputConverter.forparty2node(result), {
                                classes: {
                                    dropdown: "token-input-dropdown memberDialog_selectTree " + submitcss + ""
                                },
                                onAdd:function(item){
                                    if(organization){
                                        var $itme=$("#"+item.id,$(organization));
                                        if($itme.length){
                                            $itme.addClass("jstree-clicked");
                                            $(".jstree-anchor:first", $itme).addClass("jstree-clicked");
                                        }
                                    }
                                    if (member) {
                                        var $itme = $("#members_" + item.id, $(member));
                                        if($itme.length){

                                            $itme.addClass("selected");
                                            $("input[type='checkbox']", $itme).prop("checked",true);
                                        }
                                    }

                                },
                                onDelete:function(item){
                                    if(organization){
                                        var $itme=$("#"+item.id,$(organization));
                                        if($itme.length){
                                            $itme.removeClass("jstree-clicked");
                                            $(".jstree-anchor:first", $itme).removeClass("jstree-clicked");
                                        }
                                    }
                                    if (member) {
                                        var $itme = $("#members_" + item.id, $(member));
                                        if($itme.length){
                                            $itme.removeClass("selected");
                                            $("input[type='checkbox']", $itme).prop("checked",false);
                                        }

                                    }

                                },
                                tokenLimit: tokenLimit || 999
                            }, selected)
                            if (contact) {
                                method.get(contact, function(presult) {
                                    var set = $this.data("settings");
                                    var contact_vals = $.tokenInputConverter.formember2token(presult);
                                    set.local_data = set.local_data.concat(contact_vals);
                                    selectedContact_Checked(contact_vals);
                                }, false, part ? {
                                    department: selected,
                                    member: selectedContact
                                } : false)
                            };

                        }, false, part ? {
                            department: selected
                        } : false)
                    } else {
                        if (contact) {
                            method.get(contact, function(result) {
                                var contact_vals = $.tokenInputConverter.formember2token(result);
                                $this.tokenInputTree(contact_vals, 0, {
                                    classes: {
                                        dropdown: "token-input-dropdown memberDialog_selectTree " + submitcss + ""
                                    },
                                    onAdd: function(item) {
                                        if (member) {
                                            var $itme = $("#members_" + item.id, $(member));
                                            $itme.addClass("selected");
                                            $("input[type='checkbox']", $itme).prop("checked",true);
                                        }


                                    },
                                    onDelete: function(item) {
                                        if (member) {
                                            var $itme = $("#members_" + item.id, $(member));
                                            $itme.removeClass("selected");
                                            $("input[type='checkbox']", $itme).prop("checked",false);
                                        }

                                    },
                                    tokenLimit: tokenLimit || 999
                                }, 0)
                                selectedContact_Checked(contact_vals);
                            }, false, part ? {
                                department: selected,
                                member: selectedContact
                            } : false)
                        }
                    }
                    $this.closest('form').on("click", 'button[type="submit"],[data-toggle="method"]', function(e) {
                        if ($this.data("ruleRequired") && !$this.prev(".token-input-list").is(":hidden")) {
                            if ($this.tokenInput("get").length == 0) {
                                var str1 = "请选择{0}{1}";
                                var str2 = path ? "部门" : "";
                                var str3 = path ? (contact ? "或人员" : "") : (contact ? "人员" : "")
                                var strmsg = str1.format(str2, str3);
                                var msg = $this.data("requiredMsg")

                                config.msg.info(msg || strmsg);
                                window.formValided = false;
                                return false;
                            };
                        };
                    })
                    $(document).on("click","."+submit+" .jstree-node",function(){
                        if(!$(this).find(".jstree-disabled").length){
                            setTimeout(function(){
                                $this.closest('form').submit();
                            },200);

                        }

                    });
                })


            });
        }
        var $tokeninputtree = $('[data-toggle="tokeninputtree"]', el);
        $tokeninputtree.length > 0 && window.wm.tokeninputtree($tokeninputtree)
        var $treeinput = $('[data-toggle="treeinput"]', el);
        $treeinput.length > 0 && require.async(['jstree', 'style/plugins/jstree/jstree.min.css'], function() {
            $.each($treeinput, function(index, val) {
                var $this = $(this),
                    path = $this.data("path"),
                    selected = $this.data("selected"),
                    submit = $this.data("submit");;
                method.get(path, function(result) {
                    var d = $.tokenInputConverter.forparty2node(result, {
                        selected: selected || [-1]
                    });
                    var list = {}
                    for (var i = d.length - 1; i >= 0; i--) {
                        list[d[i].id] = d[i].text;
                    };
                    $this.jstree({
                        'core': {
                            'data': d,
                            'themes': {
                                'name': false,
                                'url': false,
                                'dir': false,
                                'icons': true,
                                'variant': false,
                                'stripes': false,
                                'responsive': true,
                                'dots': true
                            }
                        },
                        'types': {
                            'default': {
                                'icon': 'icon icon_folder_blue'
                            }
                        },
                        'plugins': ['types', 'wholerow']
                    }).on("select_node.jstree", function(e, data) {
                        $(".jstreeinput_val", el).val(data.selected);
                        var selectedlist = [];
                        for (var i = data.selected.length - 1; i >= 0; i--) {
                            selectedlist.push(list[data.selected[i]])
                        };
                        $(".jstreeinput_val", el).data("list", selectedlist);
                    })

                })
            })
            $(el).on("click", 'button[type="submit"]', function() {
                var l = $(this).closest(".form-horizontal").find(".jstreeinput_val").length
                if ($(".jstreeinput_val", el).val().length < 1 && l) {
                    config.msg.msgbox.alert("至少选择一个部门");
                    return false;
                };
            })
        });


        var $js_label_tree = $(".js_label_tree",el);
        if ($js_label_tree.length) {
            require("jstree");
            template.helper('members_checked', function(id, arr) {
                return $.inArray(id,arr) > -1 ? 'checked="checked"' : config.empty;
            });
            template.helper('members_selected', function(id, arr) {
                return $.inArray(id,arr) > -1 ? 'selected' : config.empty;
            });
            var memberstmp = require("./tmp/members.html");
            $.each($js_label_tree, function(index, val) {
                var $this = $(this),
                    path = $this.data("methodPath"),
                    selected = $this.data("selected"),
                    isedit = $this.data("edit"),
                    type=$this.data("type"),
                    target=$this.data("target");
                var checkbox="";
                if(type=="organization"){
                    checkbox="checkbox";
                }
                if (path) {
                    method.get(path.format("get_all"), function(result) {
                        var d = $.tokenInputConverter.forparty2node(result, {
                            selected: selected || [-1]
                        });
                        if(type=="members"){
                            if(result){
                                $.post(path.format("get_members"),{id:result[0].id}, function(data) {
                                    var d={},selectarr=[];
                                    d.data=data;
                                    selectarr=$(".js_tokeninputtree").val().split(",");
                                    d.select=selectarr;
                                    $(target).html(template.compile(memberstmp)(d));
                                });
                            }
                        }
                        $this.jstree({
                            'core': {
                                'data': d,
                                'check_callback': function(o, n, p, i, m) {
                                    // if (m && m.dnd && m.pos !== 'i') {
                                    //     return false;
                                    // }
                                    // if (o === "move_node" || o === "copy_node") {
                                    //     if (this.get_node(n).parent === this.get_node(p).id) {
                                    //         return false;
                                    //     }
                                    // }
                                    return true;
                                },
                                'themes': {
                                    'name': false,
                                    'url': false,
                                    'dir': false,
                                    'icons': true,
                                    'variant': false,
                                    'stripes': false,
                                    'responsive': true,
                                    'dots': false
                                }
                            },
                            "checkbox" : {
                                "three_state": false
                            },
                            'types': {
                                'default': {
                                    'icon': 'icon icon_folder_blue'
                                }
                            },
                            'plugins': ['types',checkbox,'wholerow']
                        }).on("select_node.jstree", function(e, data) {
                            //window.location = "?did={0}".format(data.node.id)
                            if(type=="organization"){
                                $(".js_tokeninputtree").tokenInput("add", {id: data.node.id,name:data.node.text});
                            }else{
                                $.post(path.format("get_members"),{id:data.node.id}, function(data) {
                                    var d={},selectarr=[];
                                    d.data=data;
                                    selectarr=$(".js_tokeninputtree").val().split(",");
                                    d.select=selectarr;
                                    $(target).html(template.compile(memberstmp)(d));
                                });
                            }

                        }).on('deselect_node.jstree', function (e, data) {
                            $(".js_tokeninputtree").tokenInput("remove", {id: data.node.id});
                        })
                    })
                }
                if(type=="members"){
                    $(el).on("click", ".listview-item", function() {
                        var $this = $(this),
                            id = $this.data("id"),
                            name = $this.data("name"),
                            thumb_path = $this.data("src"),
                            $input = $("input[type='checkbox']", $this);
                        if ($this.hasClass('selected')) {
                            $this.removeClass('selected');
                            $input.prop("checked", false);
                            $(".js_tokeninputtree").tokenInput("remove", {
                                id: id
                            });
                        } else {
                            $this.addClass('selected');
                            $input.prop("checked", true);
                            $(".js_tokeninputtree").tokenInput("add", {
                                id: id,
                                name: name,
                                thumb_path: thumb_path
                            });
                        }
                    });
                }
            });

        }
    }
});